<?php
declare(strict_types=1);

namespace App\Logic\User\Service\Image;

use App\Logic\User\Repository\Image\ImageRepository;
use App\Logic\User\Service\UserServiceInterface;
use Closure;

class ImageService implements UserServiceInterface
{

    public static function searchWhere(array $requestParams): Closure
    {
        return function ($query) use ($requestParams) {
            extract($requestParams);
            $query->where("is_show", "=", 1);
            if (!empty($uid)) {
                $query->where("uid", "=", $uid);
            }
            if (!empty($author_uid)) {
                $query->where("author_uid", "=", $author_uid);
            }
            if (!empty($series_uid)) {
                $query->where("series_uid", "=", $series_uid);
            }
            if (!empty($subject_uid)) {
                $query->where("subject_uid", "=", $subject_uid);
            }
            if (!empty($title)) {
                $query->where("title", "like", "%$title%");
            }
        };
    }

    public function serviceSelect(array $requestParams): array
    {
        return (new ImageRepository())->repositorySelect(
            self::searchWhere($requestParams),
            (int)($requestParams["size"] ?? 20),
            ["uid", "user_uid", "author_uid", "series_uid", "url", "path", "title", "download", "device_type"]
        );
    }

    public function serviceSubjectSelect(array $requestParams): array
    {
        return (new ImageRepository())->repositorySelect(
            self::searchWhere($requestParams),
            (int)($requestParams["size"] ?? 20),
            ["uid", "user_uid", "author_uid", "series_uid", "url", "path", "title", "download", "device_type"]
        );
    }

    public function serviceCreate(array $requestParams): array
    {
        return [];
    }

    public function serviceUpdate(array $requestParams): array
    {
        return [];
    }

    public function serviceDelete(array $requestParams): int
    {
        return 0;
    }

    public function serviceContent(array $requestParams): array
    {
        return (new ImageRepository())->repositoryFind(self::searchWhere($requestParams), ["content", "title"]);
    }

    /**
     * 壁纸详情
     * @param array $requestParams
     * @return array
     */
    public function serviceFind(array $requestParams): array
    {
        return (new ImageRepository())->repositoryFind(self::searchWhere($requestParams), ["uid", "title", "device_type"]);
    }
}
